<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        //(1)创建空的对象
        var computer = {};
        //创建空对象
        var computer2 = new Object();
        //给对象添加属性和方法
        //对象名.属性名 = 值;
        //对象名['属性名'] = 值;
        //对象名.方法名 = function(){ 执行语句;}
        computer.brand = '联想';
        computer.price = 6000;
        computer.memory = '16GB+1T';
        computer['color'] = '银色';
        computer.writeCode = function(){
            console.log('写js代码！');
        }
        //获取对象的属性和方法
        console.log(computer);
        console.log( computer.brand);
        console.log( computer['price']);
        //对象名.方法名();
        computer.writeCode();
        //(2)创建带有属性和方法的对象
        //键-值对 
        var car = {
            brand:'小米su7',
            price:250000,
            color:'blue',
            run:function(){
                console.log('自动驾驶');
            }
        };
        car.hyperEngine = 'V6';
        console.log(car.brand);
        console.log(car['hyperEngine']);
        car.run();
        //(3)构造函数创建对象
        function Book(bookname,author,price,press){
            this.bookname = bookname;
            this.author = author;
            this.price = price;
            this.press = press;
            this.show = function(){
                console.log('书名：'+this.bookname+'，作者：'+this.author);
                console.log('价格：'+this.price+'，出版社：'+this.press);
            }
        }
        //通过new来创建对象
        var b1 = new Book('JavaScript与jQuery实战教程','卢淑萍',59,'清华大学出版社');
        b1.show();
        var b2 = new Book('Java基础入门','黑马程序员',59.9,'清华大学出版社');
        b2.show();
        //遍历对象的属性和方法 for..in
        /*
            for(var 属性名 in 对象名){
                console.log(属性名);//当前对象所有的属性名
                console.log(对象名[属性名]);//当前对象所有属性对应的值
            }
        */
        for(var key in b1){
            //console.log(key,b1[key]);
            //先对属性值进行判断，如果是方法调用方法
            //instanceof 判断类型
            if(b1[key] instanceof Function){
                b1[key]();
            }else{
                console.log(b1[key]);
            }
        }
        //判断当前对象是否有某个属性或方法 in
        console.log('page' in b1);//false
        console.log('author' in b1);//true

    </script>
</body>
</html>